package cn.edu.sanxiau.www.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.sanxiau.www.model.User;
import cn.edu.sanxiau.www.servier.UserServier;
import cn.edu.sanxiau.www.servier.impl.UserServierImpl;
import frame.controller.BaseController;
import frame.controller.BaseController2;

@WebServlet(urlPatterns = "/userController.do")
public class UserController extends BaseController2 {

    /**
     *
     */
    private static final long serialVersionUID = 2796538909710505098L;


    // 增加
    public String addUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收数据；

        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String address = req.getParameter("address");
        String tel = req.getParameter("tel");
        String uaername = req.getParameter("uaername");
        String password = req.getParameter("password");

        //封装
        User user = new User();
        user.setName(name);
        user.setSex(sex);
        user.setAddress(address);
        user.setTel(tel);
        user.setUaername(uaername);
        user.setPassword(password);

        System.out.println("-----UserController---addUser--------" + user);

        //3.调用业务；
        int n = userServier.addUserByUser(user);


        //准备返回数据
        List<User> userList_db = userServier.queryAllUser();
        req.setAttribute("userList_db", userList_db);

        //2.跳转页面；
        return "view/system/user/userList.jsp";
    }


    //列表
    public String listUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收数据；

        //3.调用业务；
        List<User> userList_db = userServier.queryAllUser();
        for (User user_db : userList_db) {
            System.out.println("--------listUser--------" + user_db);
        }

        //响应这个数据
        req.setAttribute("userList_db", userList_db);

        //2.跳转页面；
        return "view/system/user/userList.jsp";

    }


    //删除
    public String deleteUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收数据；
        String user_idStr = req.getParameter("user_idStr");
        //字符转数字类型
        int user_id = Integer.parseInt(user_idStr);
        System.out.println("--------user_id--------" + user_id);

        //3.调用业务；
        int n = userServier.deleteUserByUser_id(user_id);


        //2.跳转页面；
        //准备返回数据
        List<User> userList_db = userServier.queryAllUser();
        req.setAttribute("userList_db", userList_db);
        return "view/system/user/userList.jsp";

    }


    //修改UI
    public String updateUIUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收数据；
        String user_idStr = req.getParameter("user_idStr");
        //字符转数字类型
        int user_id = Integer.parseInt(user_idStr);


        //3.调用业务；
        User user_db = userServier.queryUserByUser_id(user_id);
        System.out.println("--------user_db--------" + user_db);
        req.setAttribute("user", user_db);

        //2.跳转页面；
        return "view/system/user/updateUser.jsp";

    }

    //修改
    public String updateUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收数据；

        String user_idStr = req.getParameter("user_idStr");
        //字符转数字类型
        int user_id = Integer.parseInt(user_idStr);

        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String address = req.getParameter("address");
        String tel = req.getParameter("tel");
        String uaername = req.getParameter("uaername");
        String password = req.getParameter("password");

        //封装
        User user = new User();
        user.setUser_id(user_id);
        user.setName(name);
        user.setSex(sex);
        user.setAddress(address);
        user.setTel(tel);
        user.setUaername(uaername);
        user.setPassword(password);

        System.err.println("------------------updateUser------------" + user);


        //3.调用业务；
        int n = userServier.updateUserByUser(user);

        //2.跳转页面；
        //准备返回数据
        List<User> userList_db = userServier.queryAllUser();
        req.setAttribute("userList_db", userList_db);
        return "view/system/user/userList.jsp";

    }

}
